package leetcode.hot100;

public class Solution4 {

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        //先合并
        int[] merge = new int[nums1.length + nums2.length];
        int i1 = 0, i2 = 0, i = 0;
        while(i1<nums1.length && i2<nums2.length){
            if(nums1[i1]<nums2[i2]){
                merge[i++] = nums1[i1++];
            }else{
                merge[i++] = nums2[i2++];
            }
        }
        while(i1<nums1.length) merge[i++] = nums1[i1++];
        while(i2<nums2.length) merge[i++] = nums2[i2++];
        //计算中位数
        return (merge.length&1)==1?merge[merge.length/2]:(merge[merge.length/2]+merge[merge.length/2-1])/2.0;
    }
}
